package com.java.rabbitmq.producer.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

/**
 * @author Woo_home
 * @create 2020/5/27 14:30
 */

@RestController
public class AMQPController {

    // 注入 RabbitTemplate 模板
    @Autowired
    public RabbitTemplate rabbitTemplate;

    @GetMapping("/direct")
    public String sendEmail(@RequestParam Map<String, Object> param) {
        // 获取参数
        String msg = param.get("msg").toString();
        // 发送消息
        rabbitTemplate.convertAndSend("EmailExchange", "EmailRouting", msg);
        return "OK";
    }

    @GetMapping("/topic")
    public String sendBlog(@RequestParam Map<String, Object> param) {
        String msg = param.get("msg").toString();
        String routingKey = param.get("key").toString();
        rabbitTemplate.convertAndSend("BlogExchange", routingKey, msg);
        return "OK";
    }

    @GetMapping("/fanout")
    public String sendFanout(@RequestParam Map<String, Object> param) {
        String msg = param.get("msg").toString();
        rabbitTemplate.convertAndSend("FanoutExchange", null, msg);
        return "OK";
    }
}
